package com.example.zhidao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.zhidao.dao.ClassifyMapper;
import com.example.zhidao.model.po.Blog;
import com.example.zhidao.model.po.Classify;
import com.example.zhidao.service.BlogService;
import com.example.zhidao.service.ClassifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;

@Service
public class ClassifyServiceImpl extends ServiceImpl<ClassifyMapper, Classify> implements ClassifyService {

    @Resource
    ClassifyMapper classifyMapper;

    @Autowired
    BlogService blogService;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean delClassify(int id) {
        try{
           classifyMapper.deleteById(id);
            UpdateWrapper<Blog> uw=new UpdateWrapper();
            uw.eq("cid",id).set("type",0).set("cid",null).set("audit","该分类已经删除，请重新选择分类");
           blogService.update(uw);
        }catch (Exception e){
            // 回滚事务
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }

        return true;
    }
}
